﻿using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Data;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class ITXEXRPT
    {
        public void YGRPAYMENTAPP()
        {
            string msg = "";
            string sdkey = "";
            try
            {
                if (!dsSource.Tables.Contains("VAART"))
                {
                    return;
                }
                if (!dsSource.Tables.Contains("VAFARBEN"))
                {
                    return;
                }
                string src = "";
                string sgb = "";
                DataTable tbVKKOPF = dsSource.Tables["VKKOPF"];
                DataTable tbVKPOS = dsSource.Tables["VKPOS"];
                string KRS;
                string LFD;
                string POS;
                string TXTISIND;
                string str1 = "", str2 = "";
                DataRow dwVKK = tbVKKOPF.Rows[0];
                DataTable tbVKPBEST = new DataTable(), tbVKK07 = new DataTable();
                DataTable TTGAAUF = new DataTable();
                string VKPMEEH = "";
                DataRow dwVKP = tbVKPOS.Rows[0];

                string order = "";
                DataTable tbVA07 = dsSource.Tables["VAART"];
                DataTable tbVA10 = dsSource.Tables["VAFARBEN"];
                DataRow dwVA07 = null;
                double VKKQTY = 0;
                string currency = dwVKK["VKKHWAEHRSL"].ToString();
                VKPMEEH = dwVKP["VKPMEEH"].ToString();
                string VKPPREH = dwVKP["VKPPREH"].ToString();
                #region=============Header==================
                double USD2RMB = Convert.ToDouble(dwVKK["VKKKURS"].ToString()) * 0.0001;
                //src = "确认号码/Proforma Invoice：" + dwVKK["VKKAUFKRS"].ToString() + "-" + System.Convert.ToInt64(dwVKK["VKKAUFLFD"].ToString());
                order = dwVKK["VKKAUFKRS"].ToString() + "-" + Right(dwVKK["VKKAUFLFD"].ToString(), 4);
                exAgent.SetCellValue(4, 26, order, true);
                exAgent.RenameSheet(order);
                exAgent.SetCellValue(5, 26, date);
                src = dwVKK["VKKBESTNR"].ToString();
                exAgent.SetCellValue(11, 8, src);
                src = dwVKK["VKKKOMENGE"].ToString();
                exAgent.SetCellValue(120, 19, Divide(src,100));
                DataRow dwHR = null;
                var qpr = from DataRow row in dsSource.Tables["HRSTAMM"].AsEnumerable()
                          where row["HRBENID"].ToString().Trim() == dwVKK["VKKSACHBE"].ToString().Trim()
                          select row;
                foreach (DataRow row in qpr)
                {
                    dwHR = row;
                    exAgent.SetCellValue(8, 26, GetGB2312Value(dwHR["HRNNAME"].ToString().Substring(0, 6)));
                    exAgent.SetCellValue(9,26, dwHR["HRPTEL"].ToString().Trim());
                }  
                #endregion
 
                int  xStart = 19;
                int rown = 2;
                int idx = 0;
                string package = "";
                double TAmount = 0;
                int count = tbVKPOS.Rows.Count;
                foreach (DataRow dwVKPOS in tbVKPOS.Rows)
                {
                    string VKPZUGET = Divide(dwVKPOS["VKPZUGET"].ToString(), 100);
                    if (Convert.ToDouble(VKPZUGET) == 0)
                        continue;
                    string color = "";
                    dwVA07 = null;
                    EventDispatcher.Instance.RaiseStatus(msg, StatusEventType.StopProgress);
                    POS = dwVKPOS["VKPAUFPOS"].ToString();
                    exAgent.SetCellValue(xStart + idx * rown, 1, Helper.GetSubRollNum(POS));
                    exAgent.SetCellValue(xStart + idx * rown, 3, dwVKPOS["VKPART"].ToString().Trim());
                    //exAgent.SetCellValue(xStart + idx * rown, 7, dwVKPOS["VKPFARBE"].ToString().Trim());

                    #region================成份===================
                    var qVA07 = from DataRow row in tbVA07.AsEnumerable()
                                where row["VAAART"].ToString() == dwVKPOS["VKPART"].ToString()
                                select row;
                    foreach (DataRow row in qVA07)
                    {
                        dwVA07 = row;
                    }
                    if (dwVA07 != null)
                    {
                        string comp =  GetGB2312Value(Helpers.Helper.GetCompositionVA(dsSource, dwVA07, "V"));
                        //exAgent.SetCellValue(12, 6, comp);
                    }
                    #endregion
                    #region================客人花号/品名======================
                    src = dwVKPOS["VKPKDARTNR"].ToString().TrimEnd() + " " + dwVKPOS["VKPKDBEZ"].ToString().TrimEnd() + " " + dwVKPOS["VKPKDFARBNR"].ToString().TrimEnd();
                    var qvakd = from DataRow row in dsSource.Tables["VAKDBEZ"].AsEnumerable()
                                where row["VAKBKOLL"].ToString() == dwVKPOS["VKPKOLL"].ToString()
                                && row["VAKBART"].ToString() == dwVKPOS["VKPART"].ToString()
                                && row["VAKBFARBE"].ToString() == dwVKPOS["VKPFARBE"].ToString()
                                select row;
                    foreach (DataRow rkd in qvakd)
                    {
                        src =rkd["VAKBKDFARBNR"].ToString().TrimEnd();
                    }
                    exAgent.SetCellValue(xStart + idx * rown, 9, dwVKPOS["VKPFARBE"].ToString()+Environment.NewLine+ GetGB2312Value(src));
                    #endregion
                    #region================数量=================== 
                    exAgent.SetCellValue(17, 20, "数量" + Environment.NewLine + "(" + VKPMEEH + ")");
                    src = dwVKPOS["VKPMENGE1"].ToString();
                    string VKPMENGE1 = Divide(src, 100);
                    //VKKQTY += System.Convert.ToDouble(VKPMENGE1);
                    exAgent.SetCellValue(xStart + idx * rown, 20, VKPMENGE1);
                    exAgent.SetCellValue(17, 24, "成纱数量" + Environment.NewLine + "(" + VKPMEEH + ")");
                    exAgent.SetCellValue(xStart + idx * rown, 24, VKPZUGET);
                    exAgent.SetCellValue(17, 26, "已发货" + Environment.NewLine + "(" + VKPMEEH + ")");
                    src = dwVKPOS["VKPFREIGE"].ToString();
                    string VKPFREIGE = Divide(src, 100);
                    
                    exAgent.SetCellValue(xStart + idx * rown, 26, VKPFREIGE);
                    exAgent.SetCellValue(17, 27, "本次发货" + Environment.NewLine + "(" + VKPMEEH + ")");
                  
                    double ToSend=Convert.ToDouble(VKPZUGET)-Convert.ToDouble(VKPFREIGE);
                    VKKQTY += ToSend;
                    exAgent.SetCellValue(xStart + idx * rown, 26, ToSend);
                    exAgent.SetCellValue(17, 28, "单价" + Environment.NewLine + " (" + currency + "/" + VKPPREH + ")");
                    src = dwVKPOS["VKPPREIS"].ToString();
                    string price=Divide(src, 1000);
                    exAgent.SetCellValue(xStart + idx * rown, 28, price);               

                    exAgent.SetCellValue(17, 30, "金额(" + currency + ")");
                    double pvalue = Math.Round(Convert.ToDouble(price) * Convert.ToDouble(ToSend), 2);
                    TAmount += System.Convert.ToDouble(pvalue);
                    exAgent.SetCellValue(xStart + idx * rown, 30, pvalue);
                    #endregion
                    #region =================交期======================
                    //exAgent.SetCellValue(xStart + idx * rown, 28, dwVKPOS["VKPWUTERM"].ToString());
                    //exAgent.SetCellValue(130, 13,"确认:"+ dwVKPOS["VKPLIEFTERM"].ToString(), true);
                    #endregion 
 
                    idx++;
                    if (idx < count)
                    {
                        exAgent.RangeCopy(19, 20, xStart + idx * rown, xStart + idx * rown+1);
                    }

                }
                exAgent.SetCellValue(119, 24, VKKQTY, true);
                exAgent.SetCellValue(119, 30, TAmount, true);

            }
            catch (Exception ex)
            {
                msg = "有错误产生，请注意:";
                EventDispatcher.Instance.RaiseStatus(msg, StatusEventType.StopProgress);
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                msg = "指令表虽然已经产生";
                EventDispatcher.Instance.RaiseStatus(msg, StatusEventType.StopProgress);
            }
        }

    }
}
